package cn.mdmm.service.impl.singlepage;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.mdmm.entity.response.ResponseBodyToOne;
import cn.mdmm.entity.singlepage.SinglePageInfoModel;
import cn.mdmm.entity.singlepage.request.SinglePageInfoRequest;
import cn.mdmm.entity.type.TypeInfoModel;
import cn.mdmm.mapper.singlepage.SinglePageInfoMapper;
import cn.mdmm.service.singlepage.SinglePageInfoService;
import cn.mdmm.service.type.TypeInfoService;
import cn.modoumama.common.exception.RequiredException;
import cn.modoumama.common.response.Response;
import cn.modoumama.page.GridDataModel;
import cn.modoumama.page.Page;
import cn.modoumama.service.base.impl.BaseServiceImpl;


/**
* 类名称：SinglePageInfo对象定义<br>
* 类描述：单页业务实现类<br>
* 类生产方式：工具自动生成代码<br>
* 创建人：dengqiang<br>
* 创建时间：2017-08-27 09:39:55<br>
* 修改人：  <br>
* 修改时间：<br>
* 修改备注：<br>
* @version   V1.0
*/

@Service
public class SinglePageInfoServiceImpl extends BaseServiceImpl<SinglePageInfoModel,Integer> implements SinglePageInfoService{
	Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	SinglePageInfoMapper currMapper;
	@Autowired
	public void setMapper(SinglePageInfoMapper genericMapper) {
		super.setGenericMapper(genericMapper);
	}
	
	@Autowired
	private TypeInfoService typeInfoService;

	//------------------请在此添加自定义方法（开始）------------------
	public GridDataModel getGridDataModelByCondition(Page po){
		Integer type = (Integer) po.getCondition().get("type");
		List<Integer> types = new ArrayList<Integer>();
		if(type != null){
			types.add(type);
			TypeInfoModel typeInfo = typeInfoService.getTypeById(type);
			types.addAll(typeInfo.getChildrenIds());
			po.addCondition("types", types);
			po.getCondition().remove("type");
		}
		
		Integer totalCount = countByCondition(po.getCondition());
		RowBounds rowBounds = new RowBounds(po.getOffset(), po.getPageSize());
		List<SinglePageInfoModel> results = this.genericMapper.findModelsByCondition(po.getCondition(), rowBounds);
		return new GridDataModel(results, totalCount.intValue());
	}

	@Override
	public Response getSinglePageInfo(SinglePageInfoRequest request) {
		if(StringUtils.isBlank(request.getTitle())){
			throw new RequiredException("title");
		}
		ResponseBodyToOne bodyToOne = new ResponseBodyToOne();
		Map<String, Object> param = new HashMap<>();
		param.put("title", request.getTitle());
		RowBounds rowBounds = new RowBounds(0, 1);
		List<SinglePageInfoModel> results = this.genericMapper.findModelsByCondition(param, rowBounds);
		if(results != null && results.size()>0){
			currMapper.addBrowseNum(results.get(0).getId());
			bodyToOne.setData(findById(results.get(0).getId()));
		}
		return bodyToOne;
	}
	//------------------请在此添加自定义方法（结束）------------------

}
